package com.ykdl.app.main;

import java.util.ArrayList;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.support.v4.view.ViewPager.OnPageChangeListener;
import android.view.View;
import android.view.View.OnClickListener;

import com.litesuits.http.LiteHttp;
import com.ykdl.app.R;
import com.ykdl.app.Utils.VersionUdgrateUtil;
import com.ykdl.app.base.BaseActivity;
import com.ykdl.app.base.BaseFragmentActivity;
import com.ykdl.app.base.MyPagerAdapter;
import com.ykdl.app.common.MyApplication;
import com.ykdl.app.main.fragments.CustomFragment;
import com.ykdl.app.main.fragments.HomeFragment;
import com.ykdl.app.main.fragments.SettingFragment;
import com.ykdl.app.main.fragments.StatisticFragment;

public class MainActivity extends BaseFragmentActivity implements
		OnClickListener {

	private ViewPager mTVPager;
	private LiteHttp client;
	private VersionUdgrateUtil versionUdgrateUtil;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		SetContentLayout(R.layout.activity_main);
		changeSystemBar();
		initViews();
//		checkVersion();
	}

	private void checkVersion() {
		// TODO Auto-generated method stub
		versionUdgrateUtil=new VersionUdgrateUtil(getApplicationContext(), true);
		versionUdgrateUtil.checkVersionUdgrate();
	}

	private void initViews() {

		mAQ.id(R.id.bt_home).clicked(this);
		mAQ.id(R.id.bt_business).clicked(this);
		mAQ.id(R.id.bt_more).clicked(this);
		mAQ.id(R.id.bt_me).clicked(this);
		setTitle("销售目标");
		// setTitleHide();
		setLeftShowOrHidden(false);

		mTVPager = (ViewPager) findViewById(R.id.viewpager);
		client = MyApplication.getInstance().getLiteHttp();
		initFragment();
		mTVPager.setOnPageChangeListener(new OnPageChangeListener() {

			@Override
			public void onPageSelected(int arg0) {
				disposalSelect(arg0);
			}

			@Override
			public void onPageScrolled(int arg0, float arg1, int arg2) {
			}

			@Override
			public void onPageScrollStateChanged(int arg0) {
			}
		});

	}

	@Override
	public void onClick(View v) {

		switch (v.getId()) {
		case R.id.bt_home:
			mTVPager.setCurrentItem(0);
			disposalSelect(0);
			break;
		case R.id.bt_business:
			setTitle("客户维护");
			mTVPager.setCurrentItem(1);
			disposalSelect(1);
			break;
		case R.id.bt_more:
			setTitle("统计");
			mTVPager.setCurrentItem(2);
			disposalSelect(2);
			break;
		case R.id.bt_me:
			setTitle("设置");
			mTVPager.setCurrentItem(3);
			disposalSelect(3);
			break;
		}

	}

	private void initFragment() {
		// TODO Auto-generated method stub
		ArrayList mFragList = new ArrayList<Fragment>();
		Fragment homeFragment = new HomeFragment();
		Fragment customFragment = new CustomFragment();
		Fragment statisticFragment = new StatisticFragment();
		Fragment settingFragment = new SettingFragment();

		mFragList.add(homeFragment);
		mFragList.add(customFragment);
		mFragList.add(statisticFragment);
		mFragList.add(settingFragment);

		MyPagerAdapter adapter = new MyPagerAdapter(
				getSupportFragmentManager(), mFragList);
		mTVPager.setAdapter(adapter);
		mTVPager.setOffscreenPageLimit(3);
	}

	/**
	 * 导航栏状态
	 * 
	 * @param position
	 */
	private void disposalSelect(int position) {
		switch (position) {
		case 0:

			mAQ.id(R.id.iv_home).background(R.drawable.home_orange);
			mAQ.id(R.id.iv_business).background(R.drawable.custom_gray);
			mAQ.id(R.id.iv_more).background(R.drawable.tongji_gray);
			mAQ.id(R.id.iv_me).background(R.drawable.set_gray);

			mAQ.id(R.id.tv_home).textColor(
					getResources().getColor(R.color.title_bg_color));
			mAQ.id(R.id.tv_business).textColor(
					getResources().getColor(R.color.gray_home_text));
			mAQ.id(R.id.tv_more).textColor(
					getResources().getColor(R.color.gray_home_text));
			mAQ.id(R.id.tv_me).textColor(
					getResources().getColor(R.color.gray_home_text));
			setTitle("销售目标");
			setTitleShow();
			break;
		case 1:
			mAQ.id(R.id.iv_home).background(R.drawable.home_gray);
			mAQ.id(R.id.iv_business).background(R.drawable.custom_orange);
			mAQ.id(R.id.iv_more).background(R.drawable.tongji_gray);
			mAQ.id(R.id.iv_me).background(R.drawable.set_gray);

			mAQ.id(R.id.tv_home).textColor(
					getResources().getColor(R.color.gray_home_text));
			mAQ.id(R.id.tv_business).textColor(
					getResources().getColor(R.color.title_bg_color));
			mAQ.id(R.id.tv_more).textColor(
					getResources().getColor(R.color.gray_home_text));
			mAQ.id(R.id.tv_me).textColor(
					getResources().getColor(R.color.gray_home_text));
			setTitle("客户维护");
			setTitleShow();
			break;
		case 2:
			mAQ.id(R.id.iv_home).background(R.drawable.home_gray);
			mAQ.id(R.id.iv_business).background(R.drawable.custom_gray);
			mAQ.id(R.id.iv_more).background(R.drawable.tongji_orange);
			mAQ.id(R.id.iv_me).background(R.drawable.set_gray);

			mAQ.id(R.id.tv_home).textColor(
					getResources().getColor(R.color.gray_home_text));
			mAQ.id(R.id.tv_business).textColor(
					getResources().getColor(R.color.gray_home_text));
			mAQ.id(R.id.tv_more).textColor(
					getResources().getColor(R.color.title_bg_color));
			mAQ.id(R.id.tv_me).textColor(
					getResources().getColor(R.color.gray_home_text));
			setTitle("统计");
			setTitleShow();

			break;
		case 3:
			mAQ.id(R.id.iv_home).background(R.drawable.home_gray);
			mAQ.id(R.id.iv_business).background(R.drawable.custom_gray);
			mAQ.id(R.id.iv_more).background(R.drawable.tongji_gray);
			mAQ.id(R.id.iv_me).background(R.drawable.set_orange);
			setTitle("设置");
			mAQ.id(R.id.tv_home).textColor(
					getResources().getColor(R.color.gray_home_text));
			mAQ.id(R.id.tv_business).textColor(
					getResources().getColor(R.color.gray_home_text));
			mAQ.id(R.id.tv_me).textColor(
					getResources().getColor(R.color.title_bg_color));
			mAQ.id(R.id.tv_more).textColor(
					getResources().getColor(R.color.gray_home_text));
			setTitleShow();
			break;
		default:
			break;
		}
	}

}
